//
//  NextViewController.m
//  SmoothNavDemo
//
//  Created by Cloudox on 2017/3/15.
//  Copyright © 2017年 Cloudox. All rights reserved.
//

#import "AlphaViewController.h"
#import "WhiteNavController.h"
#import "AlphaViewController.h"
#import "HiddenNavController.h"
#import "NormalDarkController.h"

#import "UIViewController+Cloudox.h"
#import "UINavigationController+Cloudox.h"
#import <Masonry.h>

@interface AlphaViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (nonatomic, strong) UITableView *myTable;
@property (nonatomic, strong) UIView *myTableHeader;
@property (nonatomic, strong) UIImageView *myTableHeaderBg;

@property (nonatomic, strong) UIColor *barBGColor;
@property (nonatomic, strong) UIImage *barShadowImg;

@end

@implementation AlphaViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"AlphaViewController";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTable];
    [self.myTableHeader addSubview:self.myTableHeaderBg];
    self.myTable.tableHeaderView = self.myTableHeader;
    if (@available(iOS 11.0, *))
    {
        self.myTable.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    else
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.165 green:0.804 blue:0.804 alpha:1.000];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navBarBgAlpha = @"0.0";
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
//    self.navigationController.navigationBar.tintColor = [UIColor darkGrayColor];
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];
}

- (void)updateViewConstraints
{
    [super updateViewConstraints];
    [self.myTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(self.view);
    }];
    [self.myTableHeaderBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(self.myTableHeader);
    }];
}

#pragma mark - Methods

#pragma mark - Action
- (void)goAlphaNav
{
    AlphaViewController *controller = [[AlphaViewController alloc] init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)goBlueNormalNav
{
    NormalDarkController *controller = [[NormalDarkController alloc] init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)goWhiteNormalNav
{
    WhiteNavController *controller = [[WhiteNavController alloc] init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)goHiddenNav
{
    HiddenNavController *controller = [[HiddenNavController alloc] init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark - Getter
- (UITableView *)myTable
{
    if (_myTable == nil)
    {
        _myTable = [[UITableView alloc] init];
        _myTable.backgroundColor = [UIColor clearColor];
        _myTable.dataSource = self;
        _myTable.delegate = self;
    }
    return _myTable;
}

- (UIView *)myTableHeader
{
    if (_myTableHeader == nil)
    {
        _myTableHeader = [[UIView alloc] init];
        _myTableHeader.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 225);
    }
    return _myTableHeader;
}

- (UIImageView *)myTableHeaderBg
{
    if (_myTableHeaderBg == nil)
    {
        _myTableHeaderBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"header_bg"]];
        _myTableHeaderBg.contentMode = UIViewContentModeScaleToFill;
    }
    return _myTableHeaderBg;
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 19;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WhiteNavCell"];
    cell.textLabel.textColor = [UIColor colorWithRed:0.165 green:0.804 blue:0.804 alpha:1.000];
    cell.backgroundColor = [UIColor whiteColor];
    switch (indexPath.row)
    {
        case 0:
        {
            cell.textLabel.text = @"goWhiteNormalNav";
            break;
        }
        case 1:
        {
            cell.textLabel.text = @"goBlueNormalNav";
            break;
        }
        case 2:
        {
            cell.textLabel.text = @"goHiddenNav";
            break;
        }
        case 3:
        {
            cell.textLabel.text = @"goAlphaNav";
            break;
        }
        default:
            break;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row)
    {
        case 0:
        {
            [self goWhiteNormalNav];
            break;
        }
        case 1:
        {
            [self goBlueNormalNav];
            break;
        }
        case 2:
        {
            [self goHiddenNav];
            break;
        }
        case 3:
        {
            [self goAlphaNav];
            break;
        }
        default:
            break;
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
//    CGFloat maxAlpha = 0.999;
    CGFloat alpha = scrollView.contentOffset.y / (200);
    self.navBarBgAlpha = [NSString stringWithFormat:@"%f",alpha];
    //    if (alpha < 0)
//    {
//        CGFloat offsetY = (scrollView.contentOffset.y + scrollView.contentInset.top) * -1;
//        initialFrame.origin.y = - offsetY * 1;
//        initialFrame.origin.x = - offsetY / 2;
//        initialFrame.size.width  = SCREEN_WIDTH + offsetY;
//        initialFrame.size.height = defaultViewHeight + offsetY;
//        self.headerView.backImageView.frame = initialFrame;
//    }
//    else
//    {
//        if (alpha >= 1)
//        {
//            alpha = maxAlpha;
//        }
//        //        self.barAlpha = alpha;
//        //        [self.navigationController setNavigationBarAlpha:alpha];
//        self.kooNavBarAlpha = alpha;
//        self.nowBarAlpha = alpha;
//    }
//
//    KooProductInfo *productInfo = [KooProductDB GetKooProductInfoWithKooProductId:self.kooProductId];
//    if (alpha == maxAlpha && productInfo.outerCourseTag != KooResponseType_201)
//    {
//        UIButton *downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [downloadBtn setImage:[UIImage imageNamed:@"nav_course_download"] forState:UIControlStateNormal];
//        [downloadBtn addTarget:self action:@selector(downloadAction) forControlEvents:UIControlEventTouchUpInside];
//        downloadBtn.frame = CGRectMake(0, 0, 30, 44);
//        UIBarButtonItem *downloadItem = [[UIBarButtonItem alloc]initWithCustomView:downloadBtn];
//
//        UIButton *studyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [studyBtn setImage:[UIImage imageNamed:@"nav_course_study"] forState:UIControlStateNormal];
//        [studyBtn addTarget:self action:@selector(keepStudyAction) forControlEvents:UIControlEventTouchUpInside];
//        studyBtn.frame = CGRectMake(0, 0, 30, 44);
//        UIBarButtonItem *studyItem = [[UIBarButtonItem alloc]initWithCustomView:studyBtn];
//        self.navigationItem.rightBarButtonItems = @[studyItem,downloadItem];
//    }
//    else
//    {
//        self.navigationItem.rightBarButtonItems = nil;
//    }
}

@end
